package com.ai3d.rfid.network.proxy

import com.ai3d.rfid.base.app.appContext
import com.ai3d.rfid.network.api.AccountApi
import com.ai3d.rfid.network.interceptor.CommonHeadInterceptor
import com.ai3d.rfid.network.interceptor.TokenOutInterceptor
import com.ai3d.rfid.network.interceptor.log.LogInterceptor
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.google.gson.GsonBuilder
import me.jessyan.retrofiturlmanager.RetrofitUrlManager
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier

/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2023/1/4 11:07
 * @Description : 网络请求代理
 */
object NetworkProxy {

    val accountApi: AccountApi
        get() {
            var builder = RetrofitUrlManager.getInstance().with(OkHttpClient.Builder())
            builder.apply {
                //configuring cache configuration with max size of 10M
                cache(Cache(File(appContext.cacheDir, "cxk_cache"), 10 * 1024 * 1024))
                //添加Cookies自动持久化
                cookieJar(
                    PersistentCookieJar(
                        SetCookieCache(),
                        SharedPrefsCookiePersistor(appContext)
                    )
                )
                addInterceptor(CommonHeadInterceptor())//noted: should be added before adding LogInterceptor or the head Info will not be displayed in log
                // addInterceptor(CacheInterceptor()) // could input parameter of Days,default is 7
                addInterceptor(TokenOutInterceptor())
                addInterceptor(LogInterceptor())
                connectTimeout(60, TimeUnit.SECONDS)
                readTimeout(60, TimeUnit.SECONDS)
                writeTimeout(60, TimeUnit.SECONDS)
                hostnameVerifier(
                    HostnameVerifier { hostname, sslSession -> return@HostnameVerifier true })//dismiss the verifying,constantly return true
            }

            val retrofitBuilder = Retrofit.Builder()
                .baseUrl(DnsProxy.getBaseApiUrl())
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))

            return retrofitBuilder.build().create(AccountApi::class.java)
        }

//    fun createDownFileRetrofit(iDownloadListener: IDownloadListener): AccountApi {
//        val client = OkHttpClient.Builder()
//            .addInterceptor(DownloadInterceptor(iDownloadListener))
//            .addInterceptor(LogInterceptor())
//            .connectTimeout(60 * 5, TimeUnit.SECONDS)
//            .readTimeout(60 * 5, TimeUnit.SECONDS)
//            .writeTimeout(60 * 5, TimeUnit.SECONDS)
//            .build()
//        val gson = GsonBuilder()
//            .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
//            .serializeNulls()
//            .create()
//        return Retrofit.Builder()
//            .client(client)
//            .baseUrl(RESOURCE_ZIP_BASE_URL)
//            .addConverterFactory(GsonConverterFactory.create(gson))
//            .build().create(AccountApi::class.java)
//    }
}